import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UdpSend {
    public static void main(String[] args) {
        DatagramSocket ds = null;
        try {
            //1）创建Socket对象
            ds = new DatagramSocket();
            //2)创建一个数据报包对象DatagramPacket
            //DatagramPacket(byte[] buf, int length, InetAddress address, int port)
            //参数1:当前数据的字节数组
            //参数2:当前数据的长度
            //参数3:InetAddress:ip地址对象
            //参数4:绑定的端口号
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//读取键盘输入流
            String line;
            while ((line = br.readLine()) != null) {
                if ("886".equals(line))
                    break;
                byte[] bys = line.getBytes();
                int length = bys.length;
                //public static InetAddress getByName(String host)
                InetAddress inetAddress = InetAddress.getByName("ScienceStardeMacBook-Pro.local");
                int port = 10086;
                DatagramPacket dp = new DatagramPacket(bys, length, inetAddress, port);//建立数据包，声明长度，接收端主机，端口号
                //3)发送数据报包
                //public void send(DatagramPacket p)
                ds.send(dp);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4）释放资源
            ds.close();
        }
    }
}
